package com.lanyou.esb.servicemix.util;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTPClient;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.lanyou.cook.util.gson.GsonUtil;
import com.lanyou.esb.datadictionary.FtpHostType;
import com.lanyou.esb.exception.ServerMixException;
import com.lanyou.esb.servicemix.monitor.ESBServer;

public class ConnetServerTest {
	public static void telnetConnect(List<ESBServer> servers)
			throws ServerMixException {
		if (servers == null || servers.size() == 0) {
			return;
		}
		for (ESBServer server : servers) {
			if (!telnetConnect(server.getServerAddres(), server.getPort() + "")) {
				throw new ServerMixException("连接不上ServiceMix服务器【ip="
						+ server.getServerAddres() + ",port="
						+ server.getPort() + "】", 0);
			}
		}
	}
	
	

	/**
	 * 服务器网络配置
	 * 
	 * @param strIp
	 * @param strPort
	 * @return
	 */
	public static boolean telnetConnect(String strIp, String strPort) {
		if (StringUtils.isEmpty(strIp) || StringUtils.isEmpty(strPort))
			return false;
		Socket server = null;
		try {
			server = new Socket();
			InetSocketAddress address = new InetSocketAddress(strIp, Integer
					.valueOf(strPort).intValue());
			server.connect(address, 20000);
			return true;
		} catch (UnknownHostException e) {
			return false;
		} catch (IOException e) {
			return false;
		} finally {
			if (null != server) {
				try {
					server.close();
				} catch (IOException e) {
				}
			}
		}
	}

	
	/**
	 * 测试ftp服务器
	 * @param hostDatas
	 * @throws ServerMixException
	 */
	public static void testFtpServerConnect(String hostDatas)
			throws ServerMixException {
		JsonObject jsonDatas = GsonUtil.getDefaultJsonParser().parse(hostDatas).getAsJsonObject();
  		JsonArray jsonHostArray = jsonDatas.get("hostDatas").getAsJsonArray();
  		for(int i = 0; i < jsonHostArray.size(); i++){
  			JsonObject jsonHostData = jsonHostArray.get(i).getAsJsonObject();
  			String type = jsonHostData.get("hostType").getAsString();
  			boolean isAnmous;
  			String ip = "";
  			String port = "";
  			String userName = "";
  			String password = "";
  			if(StringUtils.equals(FtpHostType.SOURCE_HOST.ValueCode, type)){
  				isAnmous = jsonHostData.get("sourceIsAnmous").getAsBoolean();
  				if(isAnmous){
  					ip = jsonHostData.get("sourceHost").getAsString();
  	  	  			port = jsonHostData.get("sourceHostPort").getAsString();
  				}else{
  					ip = jsonHostData.get("sourceHost").getAsString();
  	  	  			port = jsonHostData.get("sourceHostPort").getAsString();
	  	  	  		userName = jsonHostData.get("sourceUserName").getAsString();
	  	  			password = jsonHostData.get("sourcePassword").getAsString();
  				}
  				
  	  			
  			}else{
  				isAnmous = jsonHostData.get("desIsAnmous").getAsBoolean();
  				if(isAnmous){
  					ip = jsonHostData.get("desHost").getAsString();
  	  	  			port = jsonHostData.get("desHostPort").getAsString();
  				}else{
  					ip = jsonHostData.get("desHost").getAsString();
  	  	  			port = jsonHostData.get("desHostPort").getAsString();
	  	  	  		userName = jsonHostData.get("desUserName").getAsString();
	  	  			password = jsonHostData.get("desPassword").getAsString();
  				}
  				
  	  			
  			}
  			
  			String returnMsg = testFtpServerConnect(isAnmous, ip, port, userName, password);
  			if(!StringUtils.equals(returnMsg, "连接成功")){
  				throw new ServerMixException(returnMsg, 0);
  			}
  		}
	}
	
	/**
	 * ftp服务器网络配置
	 * 
	 * @param strIp
	 * @param strPort
	 * @return
	 */
	public static String testFtpServerConnect(Boolean isAnmous, String ip, String port, String userName, String password) {
		if (StringUtils.isEmpty(ip)){
			return "连接不上FTP服务器，ip地址不能为空";
		}
		if(StringUtils.isEmpty(port)){
			return "连接不上FTP服务器，端口不能为空";
		}
		if(isAnmous){
		}else{
			if (StringUtils.isEmpty(userName)){
				return "连接不上FTP服务器，用户名不能为空";
			}
			
			if(StringUtils.isEmpty(password)){
				return "连接不上FTP服务器，密码不能为为空";	
			}
				
		}
			
		FTPClient client = new FTPClient();
		try {
			if(ip.indexOf("/") != -1){
				ip = ip.substring(0, ip.indexOf("/"));
			}
			client.connect(ip, Integer.parseInt(port));
			if(isAnmous){
				
			}else{
				if(!client.login(userName, password)){
					return "连接不上FTP服务器【ip="+ ip + ",port="+ port + "】，用户或密码错误";	
				}
				client.logout();
			}
			
			return "连接成功";
		} catch (SocketException e) {
			return "连接不上FTP服务器【ip="+ ip + ",port="+ port + "】，ip地址或端口错误";	
		} catch (IOException e) {
			return "连接不上FTP服务器【ip="+ ip + ",port="+ port + "】，ip地址或端口错误";	
		}finally {
			if(client.isConnected()){
				try {
					client.disconnect();
				} catch (IOException e) {
					e.printStackTrace();
					return "连接不上FTP服务器【ip="+ ip + ",port="+ port + "】，ip地址或端口错误";	
				}
			}
		}
	}
}
//	public static void main(String[] args) {
//		String strIp = "172.28.16.89";
//		String strPort = "8181";
//		System.out.println(telnetConnect(strIp, strPort));
//	}


